/**
 * 
 */
package junweixu.jreader;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * @author HiyJeain
 * 
 */
public class PdfReaderActivity extends Activity
{

    Bitmap mPageBitmap;
    Canvas mPageCanvas;
    private int mScreenWidth;
    private int mScreenHeight;
    private PdfPageFactory mPdfPageFactory;
    private PdfBookPage mPdfBookPage;
    private Button nextButton;
    private Button preButton;
    private Spinner zoomSpinner;
    PointF mOffsetF = new PointF(0, 0);
    private OnClickListener preButtonClickListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            mPdfPageFactory.prePage();
            if (mPdfPageFactory.isFirstPage())
            {
                preButton.setEnabled(false);
            }
            if (!mPdfPageFactory.isLastPage())
            {
                nextButton.setEnabled(true);
            }
            mPdfPageFactory.setZoomTimes(1);
            mPdfPageFactory.drawPageCanvas(mPageCanvas);
            mPdfBookPage.setPageBitmap(mPageBitmap);
            mPdfBookPage.invalidate();
        }
    };
    private OnClickListener nextButtonClickListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            mPdfPageFactory.nextPage();
            if (!mPdfPageFactory.isFirstPage())
            {
                preButton.setEnabled(true);
            }
            if (mPdfPageFactory.isLastPage())
            {
                nextButton.setEnabled(false);
            }
            mPdfPageFactory.setZoomTimes(-1);
            mPdfPageFactory.drawPageCanvas(mPageCanvas);
            mPdfBookPage.setPageBitmap(mPageBitmap);
            mPdfBookPage.invalidate();
        }
    };

    private void initSpinner()
    {
        zoomSpinner = (Spinner) findViewById(R.id.zoomSpinner);
        zoomSpinner.setSelection(0);
        zoomSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onNothingSelected(AdapterView<?> view)
            {
            }

            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id)
            {
                float zoomTimes = (float) (mScreenWidth / mPdfPageFactory
                        .getPageMediaWidth(1));
                switch (pos)
                {
                case 1:
                    zoomTimes = 0.25F;
                    break;
                case 2:
                    zoomTimes = 0.50F;
                    break;
                case 3:
                    zoomTimes = 0.75F;
                    break;
                case 4:
                    zoomTimes = 1.00F;
                    break;
                case 5:
                    zoomTimes = 1.25F;
                    break;
                default:
                    zoomTimes = -1;
                    break;
                }
                mPdfPageFactory.setZoomTimes(zoomTimes);
                mPdfPageFactory.drawPageCanvas(mPageCanvas);
                mPdfBookPage.setPageBitmap(mPageBitmap);
                mPdfBookPage.invalidate();
            }
        });
    }

    private void initButtons()
    {
        nextButton = (Button) findViewById(R.id.nextPdfButton);
        preButton = (Button) findViewById(R.id.prePdfButton);
        nextButton.setOnClickListener(nextButtonClickListener);
        preButton.setOnClickListener(preButtonClickListener);
        if (!mPdfPageFactory.isFirstPage())
            preButton.setEnabled(true);
        if (!mPdfPageFactory.isLastPage())
            nextButton.setEnabled(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getScrrenSize();
        setContentView(R.layout.pdf_page);
        mPdfBookPage = (PdfBookPage) findViewById(R.id.pdfPageView);
        mPageBitmap = Bitmap.createBitmap(mScreenWidth, mScreenHeight,
                Bitmap.Config.ARGB_8888);
        Intent intent = getIntent();
        String mPath = intent.getStringExtra("path");
        mPdfPageFactory = new PdfPageFactory(this, mScreenWidth, mScreenHeight,
                mPath, 1);
        mPageCanvas = new Canvas(mPageBitmap);
        try
        {

            mPdfPageFactory.openBook();
            mPdfPageFactory.drawPageCanvas(mPageCanvas);
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            Toast.makeText(this, "文件打开出错！", Toast.LENGTH_SHORT).show();
        }
        mPdfBookPage.setPageBitmap(mPageBitmap);
        initSpinner();
        initButtons();
        mPdfBookPage.setClickable(true);
        mPdfBookPage.setOnTouchListener(new OnTouchListener()
        {
            private GestureDetector g = new GestureDetector(mPdfBookPage
                    .getContext(),
                    new GestureDetector.SimpleOnGestureListener()
                    {
                        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY)
                        {
                            mOffsetF.offset((int) distanceX, (int) distanceY);
                            if (mPdfPageFactory == null)
                                return false;
                            mPdfPageFactory.ensureOffset(mOffsetF);
                            mPdfPageFactory.drawPageSliceCanvas(mPageCanvas,
                                    (int) mOffsetF.x, (int) mOffsetF.y,
                                    mScreenWidth, mScreenHeight);

                            mPdfBookPage.setPageBitmap(mPageBitmap);
                            mPdfBookPage.invalidate();

                            return true;
                        }
                    });

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (g != null)
                {
                    return g.onTouchEvent(event);
                }

                return false;
            }
        });
    }

    private void getScrrenSize()
    {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
    }

}
